<?php

namespace App\Admin\Actions\Grid\Batch\SwtDheMachine;

use App\Admin\Logic\Dhe\DheMachineLogic;
use App\Http\Logic\MachineLogic;
use Dcat\Admin\Actions\Response;
use Dcat\Admin\Grid\BatchAction;
use Illuminate\Http\Request;

class SyncMachine extends BatchAction
{
    /**
     * @return string
     */
    protected $title = '添加到设备库';

    /**
     * Handle the action request.
     *
     * @param Request $request
     *
     * @return Response
     */
    public function handle(Request $request)
    {
        $keys = $this->getKey();

        if(empty($keys)) {
            return $this->response()->error('请选择同步设备')->refresh();
        }

        $res = DheMachineLogic::asyncMachineToWarehouse($keys);

        if($res === false) {
            return $this->response()->error(MachineLogic::getErrorMessage())->refresh();
        }

        return $this->response()->success('已成功同步'.$res.'条数据')->refresh();
    }

    /**
     * @return string|array|void
     */
    public function confirm()
    {
        return '添加选中设备至设备库中';
    }

    /**
     * @return array
     */
    protected function parameters()
    {
        return [];
    }

    protected function html()
    {
        return <<<HTML
            <a {$this->formatHtmlAttributes()}><i class="feather icon-upload"></i> {$this->title} &emsp;</a>
        HTML;
    }
}
